﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PControl
{
    /// <summary>
    /// 自定义组合框控件，支持设置标题居左，居中，居右显示
    /// </summary>
    public partial class GroupBoxControl : GroupBox
    {
        /// <summary>
        /// 控件标题内容
        /// </summary>
        private String text;
        /// <summary>
        /// 控件标题对齐方式
        /// </summary>
        private StringAlignment textAlignment = StringAlignment.Near;
        /// <summary>
        /// 控件边框宽度
        /// </summary>
        private int boardWidth = 10;

        public GroupBoxControl()
        {
            InitializeComponent();
            text = this.Name;
        }

        public new String Text
        {
            get { return ""; }
            set
            {
                base.Text = "";
            }
        }

        [Description("get or set the title of this groupbox\n获取或设置组合框的标题\n取代了原来GroupBox的Text属性")]
        [Category("VerticalProgressBar")]
        [RefreshProperties(RefreshProperties.All)]
        public String Title
        {
            get { return text; }
            set
            {
                text = value;
                base.Text = "";
            }
        }
        /// <summary>
        /// 重载控件重绘方法
        /// 覆盖掉原有标题及标题外框，重新绘制
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics graphics = e.Graphics;
            StringFormat s = new StringFormat();
            s.LineAlignment = StringAlignment.Near;
            s.Alignment = this.textAlignment;
            SizeF size = graphics.MeasureString(this.text, this.Font);
            Point point = new Point(boardWidth, 0);
            RectangleF rectF = new RectangleF(point.X, point.Y, size.Width, size.Height);
            if ((int)size.Width <= this.Width - 2 * boardWidth)
            {
                switch (textAlignment)
                {
                    case StringAlignment.Near:
                        point = new Point(boardWidth, 0);
                        break;
                    case StringAlignment.Center:
                        point = new Point((this.Width - (int)size.Width) / 2, 0);
                        break;
                    case StringAlignment.Far:
                        point = new Point(this.Width - boardWidth - (int)size.Width, 0);
                        break;
                }
            }else
            {
                rectF.Width = this.Width - boardWidth*2;
            }
            rectF.Location = point;
            graphics.FillRectangle(new SolidBrush(this.BackColor), rectF);
            rectF = new RectangleF(boardWidth, 0, this.Width - 2 * boardWidth, this.Height);
            graphics.DrawString(this.text, this.Font, new SolidBrush(this.ForeColor), rectF, s);
        }

        /// <summary>
        /// 获取或设置控件标题对齐方式
        /// </summary>
        public StringAlignment TextAlignment
        {
            get { return textAlignment; }
            set { 
                textAlignment = value;
                this.Refresh();
            }
        }
    }
}
